|
DHTMLX Documentation |
function eXcell_ro(cell){ //excell name is defined here
if (cell){ //default pattern, just copy it
this.cell = cell;
this.grid = this.cell.parentNode.grid;
}
this.edit = function(){} //read-only cell doesn't have edit method
this.isDisabled = function(){ return true; } // the cell is read-only, that's why it is always in the disabled state
this.setValue=function(val){
this.setCValue(val); //actual data processing may be placed here, for now we just set value as it is
}
}
eXcell_ro.prototype = new eXcell; // nest all other methods from base class
While this snippet contains about 10 lines of code, most of it may be copy-pasted. There are only two places that need to be adjusted according to your needs:
function eXcell_button(cell){ //excell name is defined here
if (cell){ //default pattern, just copy it
this.cell = cell;
this.grid = this.cell.parentNode.grid;
}
this.edit = function(){} //read-only cell doesn't have edit method
this.isDisabled = function(){ return true; } // the cell is read-only, that's why it is always in the disabled state
this.setValue=function(val){
this.setCValue("<input type='button' value='"+val+"'>",val);
}
}
eXcell_button.prototype = new eXcell; // nest all other methods from base class
As you can see, the above mentioned snippet differs from the previous one only in two places but now you can automatically convert the incoming data into a button just by defining the column type:
grid.setColTypes("button,ro");
You can use any kind of HTML in the process of cell code generation, which allows you to place complex elements inside the grid and style them as necessary. The code mentioned below creates a complex excell type, which will open new window with details on pressing the button:
function eXcell_details(cell){ //excell name is defined here
if (cell){ //default pattern, just copy it
this.cell = cell;
this.grid = this.cell.parentNode.grid;
}
this.edit = function(){} //read-only cell doesn't have edit method
this.isDisabled = function(){ return true; } // the cell is read-only, that's why it is always in the disabled state
this.setValue=function(val){var row_id=this.cell.parentNode.idd; //get related row id
this.setCValue(val+"<input type='button' onclick='window.open(\"details.php?for=\"+row_id)'>",val);
}
}
eXcell_button.prototype = new eXcell; // nest all other methods from base class
function eXcell_button(cell){ //excell name is defined hereAs result - 3 new methods were added:
if (cell){ //default pattern, just copy it
this.cell = cell;
this.grid = this.cell.parentNode.grid;
eXcell_ed.call(this); //use methods of "ed" excell
}
this.setValue=function(val){
this.setCValue("<input type='button' value='"+val+"'>",val);
}
this.getValue=function(){
return this.cell.firstChild.value; // get button label
}
}
eXcell_button.prototype = new eXcell; // nest all other methods from base class
function eXcell_myprice(cell){ //excell name is defined hereAs you can see, only the text marked in bold was changed, and now, instead of a useless button we have the value formated with some postfix. This value can be correctly editable as Integer value.
if (cell){ //default pattern, just copy it
this.cell = cell;
this.grid = this.cell.parentNode.grid;
eXcell_ed.call(this); //use methods of "ed" excell
}
this.setValue=function(val){
this.setCValue("<span>"+val+"</span><span> USD</span>",val);
}
this.getValue=function(){
return this.cell.childNodes[0].innerHTML; // get value
}
}
eXcell_button.prototype = new eXcell; // nest all other methods from base class
function eXcell_mytime(cell){ //excell name is defined here
if (cell){ //default pattern, just copy it
this.cell = cell;
this.grid = this.cell.parentNode.grid;
}
this.setValue=function(val){
this.setCValue(val);
}
this.getValue=function(){
return this.cell.innerHTML; // get value
}
this.edit=function(){
this.val = this.getValue(); //save current value
this.cell.innerHTML="<input type='text' style='width:50px;'><select style='width:50px;'><option value='AM'>AM<option value='PM'>PM</select>"; // editor's html
this.cell.firstChild.value=parseInt(val); //set the first part of data
if (val.indexOf("PM")!=-1) this.cell.childNodes[1].value="PM";
this.cell.childNodes[0].onclick=function(e){ (e||event).cancelBubble=true; } //block onclick event
this.cell.childNodes[1].onclick=function(e){ (e||event).cancelBubble=true; } //block onclick event
}
this.detach=function(){
this.setValue(this.cell.childNodes[0].value+" "+this.cell.childNodes[1].value); //set the new value
return this.val!=this.getValue(); // compare the new and the old values
}
}
eXcell_button.prototype = new eXcell; // nest all other methods from base class